package com.gomcarter.frameworks.fsm.injector.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;

/**
 * @author 李银
 * @since 2020年09月25日13:30:49
 * @deprecated 停止使用yfcloud-base-starter-fsm，推荐使用yfcloud-controller-starter-statemachine
 */
@Slf4j
public class FsmUpdateStateUpdater extends AbstractMethod {

    public FsmUpdateStateUpdater() {
        super("fsmUpdateStateUpdater");
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sqlTemplate = "<script>\nUPDATE %s SET state = #{state} where id = #{id}\n</script>";
        Configuration configuration = tableInfo.getConfiguration();
        String sql = String.format(sqlTemplate, tableInfo.getTableName());
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, "$fsmUpdateState", sqlSource, tableInfo);
    }
}
